package com.example.tianqi.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.tianqi.temperatureAdapter;
import com.example.tianqi.R;
import com.example.tianqi.weather_listAdapter;
import com.example.tianqi.weatherAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private AppCompatImageView addImageView;
    private RecyclerView mainRecyclerView;
    private RecyclerView tianqiqingkuangRecyclerView;
    private AppCompatTextView cityTextView;
    private RecyclerView dqwenduRecyclerView;
    private AppCompatTextView shijian2TextViewt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addImageView = findViewById(R.id.addImageView);
        mainRecyclerView = findViewById(R.id.mainRecyclerView);
        tianqiqingkuangRecyclerView = findViewById(R.id.tianqiqingkuangRecyclerView);
        cityTextView = findViewById(R.id.cityTextView);
        dqwenduRecyclerView = findViewById(R.id.dqwenduRecyclerView);
        shijian2TextViewt = findViewById(R.id.shijian2TextViewt);

        addImageView.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, CityActivity.class);
            startActivityForResult(intent, 1);
        });


        OkGo.<String>get("https://v0.yiketianqi.com/api?unescape=1&version=v9&appid=65716696&appsecret=pPQF7EgL")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject1 = new JSONObject(response.body());
                            // 取当前城市
                            cityTextView.setText(jsonObject1.getString("city"));

                            // 取当前日期
                            Calendar cal = Calendar.getInstance();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年 MM月d日");
                            String strDate = sdf.format(cal.getTime());
                            shijian2TextViewt.setText(strDate);

                            ArrayList<JSONObject> arrayList = new ArrayList<>();
                            JSONObject jsonObject = new JSONObject(response.body());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                arrayList.add(jsonArray.getJSONObject(i));
                            }
                            Log.d("TAGS", arrayList.get(0).toString());

                            weather_listAdapter adapter = new weather_listAdapter(arrayList);
                            weatherAdapter adapter1 = new weatherAdapter(arrayList);
                            temperatureAdapter adapter2 = new temperatureAdapter(arrayList);

                            // RecyclerView的简单设置
                            // 七天天气
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this) {
                                @Override
                                public boolean canScrollVertically() {
                                    return false;
                                }
                            };
                            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);   //将布局设置为垂直
                            mainRecyclerView.setLayoutManager(linearLayoutManager);

                            // 禁止水平滑动  天气情况
                            LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(MainActivity.this) {
                                @Override
                                public boolean canScrollHorizontally() {
                                    return false;
                                }
                            };
                            linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);    //将布局设置为水平
                            tianqiqingkuangRecyclerView.setLayoutManager(linearLayoutManager1);

                            // 当前温度
                            LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(MainActivity.this) {
                                @Override
                                public boolean canScrollVertically() {
                                    return false;
                                }
                            };
                            linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);    //将布局设置为垂直
                            dqwenduRecyclerView.setLayoutManager(linearLayoutManager2);


                            //将适配器与RecyclerView绑定
                            mainRecyclerView.setAdapter(adapter);
                            tianqiqingkuangRecyclerView.setAdapter(adapter1);
                            dqwenduRecyclerView.setAdapter(adapter2);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }
//在CityAddActivity里的数据跳转 并展示出来在这个界面
    @Override
    protected void onActivityResult(int req, int res, @Nullable Intent data) {
        super.onActivityResult(req, res, data);
        if (res == RESULT_OK && req == 1 && data != null) {
            String city = data.getStringExtra("city");
            Toast.makeText(this,city,Toast.LENGTH_SHORT).show();

            OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=65716696&appsecret=pPQF7EgL&city=" +city )
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            try {
                                JSONObject jsonObject1 = new JSONObject(response.body());
                                // 取当前城市
                                cityTextView.setText(jsonObject1.getString("city"));

                                // 取当前日期
                                Calendar cal = Calendar.getInstance();
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年 MM月d日");
                                String strDate = sdf.format(cal.getTime());
                                shijian2TextViewt.setText(strDate);

                                ArrayList<JSONObject> arrayList = new ArrayList<>();
                                JSONObject jsonObject = new JSONObject(response.body());
                                JSONArray jsonArray = jsonObject.getJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    arrayList.add(jsonArray.getJSONObject(i));
                                }
                                Log.d("TAGS", arrayList.get(0).toString());

                                weather_listAdapter adapter = new weather_listAdapter(arrayList);
                                weatherAdapter adapter1 = new weatherAdapter(arrayList);
                                temperatureAdapter adapter2 = new temperatureAdapter(arrayList);

                                // RecyclerView的简单设置
                                // 七天天气
                                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this) {
                                    @Override
                                    public boolean canScrollVertically() {
                                        return false;
                                    }
                                };
                                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);   //将布局设置为垂直
                                mainRecyclerView.setLayoutManager(linearLayoutManager);

                                // 禁止水平滑动  天气情况
                                LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(MainActivity.this) {
                                    @Override
                                    public boolean canScrollHorizontally() {
                                        return false;
                                    }
                                };
                                linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);    //将布局设置为水平
                                tianqiqingkuangRecyclerView.setLayoutManager(linearLayoutManager1);

                                // 当前温度
                                LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(MainActivity.this) {
                                    @Override
                                    public boolean canScrollVertically() {
                                        return false;
                                    }
                                };
                                linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);    //将布局设置为垂直
                                dqwenduRecyclerView.setLayoutManager(linearLayoutManager2);


                                //将适配器与RecyclerView绑定
                                mainRecyclerView.setAdapter(adapter);
                                tianqiqingkuangRecyclerView.setAdapter(adapter1);
                                dqwenduRecyclerView.setAdapter(adapter2);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

        }
    }

}
